package com.rivercloud.dao.Impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.poi.poifs.nio.DataSource;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.rivercloud.dao.TableColDao;
import com.rivercloud.entity.TableCol;

public class TableColDaoImpl  implements TableColDao{
	static SqlSessionFactory sqlSessionFactory = null;

	static {
		SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}

		sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
	}

	@Override
	public void addTableCol(TableCol tableCol) {
		SqlSession session = sqlSessionFactory.openSession();
		session.insert("addTableCol",  tableCol);
		session.commit();
		session.close();
	}

	@Override
	public List<TableCol> findByTableId( int tableId) {
		try {
			SqlSession session = sqlSessionFactory.openSession();
			List<TableCol> list =  session.selectList("findByTableId", tableId);
			
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return null;
	}

	@Override
	public List<Integer> findColNumByTableId(int tableId) {
		SqlSession session = sqlSessionFactory.openSession();
		List<TableCol> listTableCol = session.selectList("findColNumByTableId",  tableId);
		List<Integer> list = new ArrayList<Integer>();
		for (TableCol tableCol : listTableCol) {
			list.add(tableCol.getColNum());
		}
		session.close();
		return list;
	}

	@Override
	public void deleteTableColByTableId(Integer tableId) {
		SqlSession session = sqlSessionFactory.openSession();
		session.delete("deleteTableColByTableId", tableId);
		session.commit();
		session.close();
		
	}

}
